package com.miplus.ccrm.core.service.strategy;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.core.controller.vo.ficase.ImpResultVo;
import com.miplus.ccrm.core.controller.vo.strategy.CpDoReqVo;
import com.miplus.ccrm.core.mapper.CfgActivityMapper;
import com.miplus.ccrm.core.mapper.CfgAssignsetMapper;
import com.miplus.ccrm.core.mapper.CpCalllistMapper;
import com.miplus.ccrm.core.mapper.CpCalllistTriggerMapper;
import com.miplus.ccrm.core.pojo.CfgActivity;
import com.miplus.ccrm.core.pojo.CfgAssignset;
import com.miplus.ccrm.core.pojo.CpCalllist;
import com.miplus.ccrm.core.pojo.CpCalllistTrigger;
import com.miplus.ccrm.core.service.aicall.ObCallCaseService;
import com.miplus.ccrm.core.service.aiqm.QmDataService;
import com.miplus.ccrm.core.service.iou.FiCasesServiceIou;
import com.miplus.ccrm.core.utils.EnumFiChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class CampaignService {
    @Resource
    private CpCalllistTriggerMapper triggerMapper;
    @Resource
    private CpCalllistMapper cpCalllistMapper;
    @Resource
    private ObCallCaseService obCallCaseService;
    @Resource
    private QmDataService qmDataService;
    @Resource
    private FiCpdoTraceService traceService;
    @Resource
    private FiCasesServiceIou fiCasesServiceIou;
    @Resource
    private CfgAssignsetMapper assignsetMapper;
    @Resource
    private CfgActivityMapper activityMapper;

    public CmdResVo cpTaskDo(JSysUser user, String reqhost, CpDoReqVo paras)
    {
        int ret=0;
        String usrid=user.getUsrid();
        String tasktype=paras.getTasktype();
        String taskid=paras.getTaskid();
        CmdResVo resVo=new CmdResVo();
        resVo.setCount(-1);

        String taskname="-";
        CpCalllist calllist=null;
        CfgAssignset assignset=null;
        if (JPubUtils.IsEqual(tasktype, "cptask")) {
            calllist = cpCalllistMapper.selectById(taskid);
            String triggerid = calllist.getTriggerid();
            if (JPubUtils.IsEmpty(triggerid)) {
                log.info("cpTaskDo({}).nofind triggerid", taskid);
                return resVo;
            }
            taskname=calllist.getName();
        }
        else if (JPubUtils.IsEqual(tasktype, "dist")) {
            CfgActivity activity = activityMapper.selectById(JPubUtils.StringToInt(taskid));
            if (activity == null){
                log.warn("cpTaskDo({}).nofind activity",taskid);
                return new CmdResVo(0);
            }
            taskname=activity.getName();
        }
        else if (JPubUtils.IsEqual(tasktype, "ass")) {
            assignset = assignsetMapper.selectById(taskid);
            if (JPubUtils.IsEmpty(assignset)) {
                log.info("cpTaskDo({}).nofind assignset", taskid);
                return resVo;
            }
            taskname=assignset.getRemark();
        }
        Integer traceid=traceService.writeTrace(usrid,taskid,taskname,
                "cptask",paras.getDomode(),reqhost,"process"
        );
        try {
            if (JPubUtils.IsEqual(tasktype, "cptask")) {
                ret = cpTaskDo(usrid, calllist);
                resVo.setCount(ret);
            } else if (JPubUtils.IsEqual(tasktype, "dist")) {
                resVo = fiCasesServiceIou.caseDistribute(
                        user, JPubUtils.StringToInt(paras.getTaskid()),
                        paras.getDomode(),null
                );
            }
            else if (JPubUtils.IsEqual(tasktype, "ass")) {
                resVo = fiCasesServiceIou.caseAssign(
                        user, taskid,
                        paras.getDomode(),
                        assignset.getModeval()
                );
            }
            traceService.writeResult(traceid, "success", "数量：" + String.format("%d", ret));
        }
        catch(Exception e){
            log.warn("cpTaskDo.Error:{}",e);
            traceService.writeResult(traceid,"fail","数量："+String.format("%d",ret));
        }

        return resVo;
    }

    //执行任务
    public int cpTaskDo(String usrid, CpCalllist calllist)
    {
        String clid=calllist.getClid();
        Integer triggerid=JPubUtils.StringToInt(calllist.getTriggerid());
        CpCalllistTrigger trigger=triggerMapper.selectById(triggerid);
        if(trigger==null){
            log.info("runCampaign({}).nofind triggerid={}",clid,triggerid);
            return 0;
        }
        int ret=0;
        EnumFiChannel enChannel=EnumFiChannel.AsFiChannel(calllist.getChannel());
        if (enChannel == EnumFiChannel.AiCall) {
            ret = doAiCallIInput(usrid, clid, triggerid.toString());
        } else if (enChannel == EnumFiChannel.AiQm) {
            ret = doAiQmIInput(usrid, clid, triggerid.toString(), calllist.getSendmodel());
        }

        return ret;
    }

    private int doAiCallIInput(String usrid,String clid,String triggerid)
    {
        ImpResultVo resultVo=obCallCaseService.impObCase(
                usrid,clid,triggerid,
                "执行AI外呼取数","trigger"
        );
        return resultVo.getOkcnt();
    }

    private int doAiQmIInput(String usrid,String clid,String triggerid,String tempid)
    {
        ImpResultVo resultVo=qmDataService.importQmData(
                usrid,clid,triggerid,
                "执行AI质检取数","trigger",tempid
        );
        return resultVo.getOkcnt();
    }
}
